<?php namespace Phpcmf\Controllers;

class Home extends \Phpcmf\App
{

    private $app_config;

    public function __construct(...$p) {
        parent::__construct($p);
        $this->app_config = \Phpcmf\Service::M('app')->get_config(APP_DIR);
        if (!$this->app_config['zs']) {
            $this->app_config['zs'] = 'https://www.kaiqibao.net/css2019/pcms/zs.jpg';
        } else {
            $this->app_config['zs'] = dr_get_file($this->app_config['zs']);
        }

        \Phpcmf\Service::V()->assign([
            'meta_title' => $this->app_config['title'],
            'meta_keywords' => $this->app_config['keywords'],
            'meta_description' => $this->app_config['description'],
        ]);
    }

    public function index() {
        \Phpcmf\Service::V()->display('domain_search.html');
    }

    public function show() {

        $domain = dr_safe_replace(str_replace(['http://', 'https://'], '', \Phpcmf\Service::L('Input')->get('domain')));
        if (!$domain) {
            $this->_error(0, '域名为空，无法查询');
        }

        if (is_file(MYPATH.'Config/License.php')) {
            $license = require MYPATH.'Config/License.php';
            if (!$license['license']) {
                $this->_error(0, '程序不是最新，请在官网下载[安装包]并覆盖src目录');
            }
        } else {
            $this->_error(0, '程序不是最新，请在官网下载[安装包]并覆盖src目录');
        }

        $api = 'https://www.kaiqibao.net/license.php?at=api&domain='.$domain.'&id='.$license['license'];
        $rt = dr_catcher_data($api);
        if (!$rt) {
            $this->_error(0, '无法访问服务端');
        }

        $rt = dr_string2array($rt);
        if (!$rt['code']) {
            $this->_error(0, '查询结果：'.$rt['msg']);
        }

        \Phpcmf\Service::V()->assign([
            'zs' => $this->app_config['zs'],
            'license' => $rt['data'],
        ]);
        \Phpcmf\Service::V()->display('domain_show.html');
    }

    private function _error($code, $msg) {
        \Phpcmf\Service::V()->assign([
            'error_msg' => $msg,
        ]);
        \Phpcmf\Service::V()->display('domain_error.html');exit;
    }

}
